package com.edward.service;


import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.edward.PagerModel;
import com.edward.model.ActHarvest;


public class ActHarvestManager extends HibernateDaoSupport {
	
	public void addActHarvest(ActHarvest actHarvest) {
		
		getHibernateTemplate().save(actHarvest);
	}
	
	public void updateActHarvest(ActHarvest actHarvest) {
		
		getHibernateTemplate().update(actHarvest);
	}
	
	public ActHarvest getActHarvest(int id){
		ActHarvest d =(ActHarvest)getHibernateTemplate().get(ActHarvest.class, id);
		return d;
	}
	

	@SuppressWarnings("unchecked")
	public List<ActHarvest> listActHarvest() {
		return getHibernateTemplate().find("from ActHarvest");
	}

	@SuppressWarnings("unchecked")	
	public String addAct (Date date){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String d = sdf.format(date);
		List <ActHarvest> ActList = getHibernateTemplate().find("from ActHarvest s where s.type is null and s.date = '" + d + "'");
		if(ActList.size()>0){
			return "以存在对应数据，添加失败!";
		}
		
			ActHarvest act = new ActHarvest();
			List <ActHarvest> l = new ArrayList<ActHarvest>();
			act.setDate(date);
			act.setActs(l);
			addActHarvest(act);
			for(int i=0; i<5;i++){
					ActHarvest st = new ActHarvest();
					st.setType(judge(i));
					st.setDate(date);
					addActHarvest(st);
					act.getActs().add(st);
			}
			updateActHarvest(act);
			return "添加成功"; 
		}
	
	@SuppressWarnings("unchecked")	
	public void updateAct (ActHarvest a){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String d = sdf.format(a.getDate());
		List <ActHarvest>l = getHibernateTemplate().find("from ActHarvest s where s.type is null and s.date = '" + d + "'");
		ActHarvest ah = l.get(0);
		ActHarvest at = ah.getActs().get(judge(a.getType())) ;
		ah.setSale(ah.getSale() + a.getSale() - at.getSale() );
		ah.setActRate(ah.getActRate() + a.getActRate() - at.getActRate());
		ah.setDiffPrice(ah.getDiffPrice() + a.getDiffPrice() - at.getDiffPrice());
		ah.setNetProfit(ah.getNetProfit() + a.getNetProfit() - at.getNetProfit() );
		at.setSale(a.getSale());
		at.setActRate(a.getActRate());
		at.setDiffPrice( a.getDiffPrice());
		at.setNetProfit(a.getNetProfit());
		updateActHarvest(ah);
		updateActHarvest(at);
	}
		
	
	

	
	@SuppressWarnings("unchecked")	
	public PagerModel pagerList(int start,int limit,String query) {
		if(query.equals("")){
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
			query = sdf.format(new Date());
		}
		List <ActHarvest>l = getHibernateTemplate().find("from ActHarvest s where s.type is null and year(s.date) = '" + query + "' order by s.date");
		List <ActHarvest> list = new ArrayList<ActHarvest>();
		ActHarvest ah = new ActHarvest();
		for(ActHarvest act : l ){
			for(ActHarvest a :act.getActs()){
				list.add(a);
			}
			ah.setSale(ah.getSale() + act.getSale() );
			ah.setActRate(ah.getActRate() + act.getActRate());
			ah.setDiffPrice(ah.getDiffPrice() + act.getDiffPrice());
			ah.setNetProfit(ah.getNetProfit() + act.getNetProfit());
			ah.setDate(null);
			act.setActs(null);
			list.add(act);
		}
		list.add(ah);
		int total = list.size();
		int toIndex = start + limit;
		if(toIndex>total){
			toIndex =  total;
		}
		List <ActHarvest>datas = list.subList(start, toIndex);
		PagerModel pm = new PagerModel();
		pm.setTotal(total);
		pm.setDatas(datas);
		return pm;
	}
	

	
	public int judge (String s ){
		if(s.equals("汽车")){
			return 0;
		}else if (s.equals("摩托车")){
			return 1;
		}else if (s.equals("汽车配件")){
			return 2;
		}else if (s.equals("摩托车配件")){
			return 3;
		}else{
			return 4;
		}
	}
	
	public String judge (int i){
		if(i == 0){
			return "汽车";
		}else if (i == 1){
			return "摩托车";
		}else if (i == 2){
			return "汽车配件";
		}else if (i == 3){
			return "摩托车配件";
		}else{
			return "其他";
		}
	}
	


}



